# created: yuyh2
# by: 2020/06/12

"""
数据类型判断
"""

# import
from decimal import Decimal

# function
def is_decimal(iterable):
    """
    功能：判断可迭代的数据(pd.Series list)是否为decimal类型
    :param iterable: pd.Series list
    :return: boolean
    """
    is_true = True
    for value in iterable:
        if not isinstance(value, Decimal):
            is_true = False
            break
    return is_true

def decimal_to_float(data):
    """
    功能：将decimal转换成float
    :param data: pd.Series
    :return: pd.Series
    """

    data_trans = data
    is_true = is_decimal(data)
    if is_true:
        data_trans = [float(i) for i in data]
    return data_trans

def df_decimal_to_float(df):
    """
    功能：将mysql、hive中decimal格式转换为float格式
    :param df: pd.DataFrame
    :return: pd.DataFrame
    """
    res = df.apply(decimal_to_float, axis = 0)
    return res


if __name__ == '__main__':
    # test
    x = [Decimal(20), Decimal(10)]
    is_decimal(x)
    decimal_to_float(x)



